
import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.swing.JTextField;
import java.beans.*; //property change stuff
import java.awt.*;
import java.awt.event.*;

/* 1.4 example used by DialogDemo.java. */
class Custom2Dialog extends JDialog
                   implements ActionListener,
                              PropertyChangeListener {
    private String typedText = null;
    private JTextField textFieldFrom;
    private JTextField textFieldTo;
    private JTextField textFieldName;
    private GpsGen dd;

    private String magicWord;
    private JOptionPane optionPane;

    private String btnString1 = "Enter";
    private String btnString2 = "Cancel";
    
    private int type;
    private int typedFrom;
    private int typedTo;
    private String typedName;

    /**
     * Returns null if the typed string was invalid;
     * otherwise, returns the string as the user entered it.
     */
    public String getValidatedName() {
        return typedName;
    }

   public int getValidatedNumberFrom() {
        return typedFrom;
    }

    public int getValidatedNumberTo() {
        return typedTo;
    }

   /** Creates the reusable dialog. */
    public Custom2Dialog(Frame aFrame, int type, String title,
    		String msgString1, String msgString2, GpsGen parent) {
        super(aFrame, true);
        dd = parent;
        setTitle(title);
        this.type = type;

        textFieldFrom = new JTextField(5);
        textFieldTo = new JTextField(5);
        textFieldName = new JTextField(15);

        //Create an array of the text and components to be displayed.
        Object[] array = {msgString1, msgString2, textFieldFrom, textFieldTo, textFieldName};

        //Create an array specifying the number of dialog buttons
        //and their text.
        Object[] options = {btnString1, btnString2};

        //Create the JOptionPane.
        optionPane = new JOptionPane(array,
                                    JOptionPane.QUESTION_MESSAGE,
                                    JOptionPane.YES_NO_OPTION,
                                    null,
                                    options,
                                    options[0]);

        //Make this dialog display it.
        setContentPane(optionPane);

        //Handle window closing correctly.
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                /*
                 * Instead of directly closing the window,
                 * we're going to change the JOptionPane's
                 * value property.
                 */
                    optionPane.setValue(new Integer(
                                        JOptionPane.CLOSED_OPTION));
            }
        });

        //Ensure the text field always gets the first focus.
        addComponentListener(new ComponentAdapter() {
            public void componentShown(ComponentEvent ce) {
                textFieldFrom.requestFocusInWindow();
            }
        });

        //Register an event handler that puts the text into the option pane.
        textFieldFrom.addActionListener(this);
        textFieldTo.addActionListener(this);
        textFieldName.addActionListener(this);

        //Register an event handler that reacts to option pane state changes.
        optionPane.addPropertyChangeListener(this);
    }

    /** This method handles events for the text field. */
    public void actionPerformed(ActionEvent e) {
    	optionPane.setValue(btnString1);
    }

    /** This method reacts to state changes in the option pane. */
    public void propertyChange(PropertyChangeEvent e) {
    	String prop = e.getPropertyName();

    	if (isVisible()
    			&& (e.getSource() == optionPane)
    			&& (JOptionPane.VALUE_PROPERTY.equals(prop) ||
    					JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
    		Object value = optionPane.getValue();

    		if (value == JOptionPane.UNINITIALIZED_VALUE) {
    			//ignore reset
    			return;
    		}

    		//Reset the JOptionPane's value.
    		//If you don't do this, then if the user
    		//presses the same button next time, no
    		//property change event will be fired.
    		optionPane.setValue(
    				JOptionPane.UNINITIALIZED_VALUE);

	    	if (btnString1.equals(value)) {
	   			String typedTextFrom = textFieldFrom.getText();
	   			String typedTextTo = textFieldTo.getText();
	   			typedName = textFieldName.getText();
//	    		System.out.println(typedTextFrom + " - " + typedTextTo);
	   		    			try {
    				typedFrom = Integer.valueOf(typedTextFrom.trim()).intValue();
    				clearAndHide();
    			} catch (NumberFormatException nfe) {
    				System.out.println("NumberFormatException: " + nfe.getMessage());
    				textFieldFrom.selectAll();
    				JOptionPane.showMessageDialog(
    						Custom2Dialog.this,
    						"Sorry, \"" + typedTextFrom + "\" "
    						+ "isn't a number.\n"
    						+ "Please try again",
    						"aaa",
    						JOptionPane.ERROR_MESSAGE);
    				typedText = null;
    				textFieldFrom.requestFocusInWindow();
    			}
    			try {
    				typedTo = Integer.valueOf(typedTextTo.trim()).intValue();
    				clearAndHide();
    			} catch (NumberFormatException nfe) {
    				System.out.println("NumberFormatException: " + nfe.getMessage());
    				textFieldTo.selectAll();
    				JOptionPane.showMessageDialog(
    						Custom2Dialog.this,
    						"Sorry, \"" + typedTextTo + "\" "
    						+ "isn't a number.\n"
    						+ "Please try again",
    						"bbb",
    						JOptionPane.ERROR_MESSAGE);
    				typedText = null;
    				textFieldTo.requestFocusInWindow();
    			}
    		} else { //user closed dialog or clicked cancel
    			dd.setResult(type, -999.0);
    			typedText = null;
    			clearAndHide();
    		}
    	}
    }

    /** This method clears the dialog and hides it. */
    public void clearAndHide() {
        textFieldFrom.setText(null);
        textFieldTo.setText(null);
        setVisible(false);
    }
}